package com.lisen.common.base.dao.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.util.Assert;

import com.lisen.common.base.dao.Dao;

public class BaseDaoHibernate extends HibernateDaoSupport implements Dao {

	public BaseDaoHibernate() {
	}

	public void saveObject(Object o) {
		getHibernateTemplate().save(o);
	}

	public Object getObject(Class<?> clazz, Serializable id) {
		Object o = getHibernateTemplate().get(clazz, id);
		if (o == null)
			throw new ObjectRetrievalFailureException(clazz, id);
		else
			return o;
	}

	public List<?> getObjects(Class<?> clazz) {
		return getHibernateTemplate().loadAll(clazz);
	}

	public void removeObject(Class<?> clazz, Serializable id) {
		getHibernateTemplate().delete(getObject(clazz, id));
	}

	public void updateObject(Object obj) {
		getHibernateTemplate().update(obj);
	}

	public Query createQuery(String hql, Object... values) {
		Assert.hasText(hql);
		Query query = getSession().createQuery(hql);
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				query.setParameter(i, values[i]);
			}
		}
		return query;
	}

	public List<?> list(String sql, Class<?> moduleClass) {
		Assert.hasText(sql);
		List<?> list = getSession().createSQLQuery(sql).addEntity(moduleClass)
				.list();
		return list;
	}

	protected boolean hasDistinct(String sql) {
		return sql.toLowerCase().indexOf("select distinct") >= 0;
	}

	/**
	 * 批量删除
	 * 
	 * @param clazz
	 * @param ids
	 */
	public void removeObjectBatch(Class<?> clazz, Serializable[] ids) {
		for (Serializable id : ids) {
			this.removeObject(clazz, id);
		}
	}

	/**
	 * Render the <tt>rownumber() over ( .... ) as rownumber_,</tt> bit, that
	 * goes in the select list
	 */
	/*
	protected String getRowNumber(String sql) {
		StringBuffer rownumber = new StringBuffer(50).append(" count(*) ");
		return rownumber.toString();
	}

	public String getLimitString(String sql, boolean hasOffset) {
		int startOfSelect = sql.toLowerCase().indexOf("select");
		StringBuffer pagingSelect = new StringBuffer(sql.length() + 100)
				.append(sql.substring(0, startOfSelect)) // add the comment
				.append("select ") // nest the main query in an outer select
				.append(getRowNumber(sql)); // add the rownnumber bit into the
		// outer query select list
		if (hasDistinct(sql)) {
			pagingSelect.append(" from ( ") // add another (inner)
					// nested select
					.append(sql.substring(startOfSelect)) // add the main
					// query
					.append(" )"); // close off the inner nested select
		} else {
			pagingSelect.append(sql.substring(
					sql.toLowerCase().indexOf("from"), sql.toLowerCase()
							.indexOf("order by") <= 0 ? sql.length() : sql
							.toLowerCase().indexOf("order by"))); // add the
			// main
			// query
		}
		return pagingSelect.toString();
	}
*/
	/**
	 * 原使用listObject分页
	 * 
	 * @param sql
	 * @param moduleClass
	 * @param listObject
	 * @throws Exception
	 */
	/*
	protected void listPage(String sql, Class<?> moduleClass,
			ListObject listObject) {
		int resultsCount = 0;
		String integer = getSession()
				.createSQLQuery(getLimitString(sql, false)).list().get(0)
				.toString();
		if (integer != null) {
			resultsCount = Integer.valueOf(integer).intValue();
		}
		SQLQuery sqlQuery = getSession().createSQLQuery(sql).addEntity(
				moduleClass);
		listObject.setCount(resultsCount);
		sqlQuery.setFirstResult((listObject.getCurrent() - 1 < 0 ? 0
				: listObject.getCurrent() - 1)
				* listObject.getRowCount());
		sqlQuery.setMaxResults((listObject.getCurrent() + listObject
				.getRowCount()) > listObject.getRowCount() ? listObject
				.getRowCount() : (listObject.getCurrent() + listObject
				.getRowCount()));
		listObject.setFormList(sqlQuery.list());
	}
	*/
}
